/**
 * 
 */
package com.ibm.framework.exception.utils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * 获取主机名的工具类
 * 
 */
public class NetUtil {

    /**
     * 获取本地ip列表
     * 
     * @return 本地ip列表
     */
    public static List<String> getAllLocalIP() {
        List<String> localIPs = new ArrayList<String>();
        Enumeration<NetworkInterface> allNetInterfaces = null;
        InetAddress ip = null;
        try {
            allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = (InetAddress) addresses.nextElement();
                    if (!ip.isLoopbackAddress() && ip instanceof Inet4Address) {
                        localIPs.add(ip.getHostAddress());
                    }
                }
            }
        } catch (Exception e) {
            e.toString();
        }
        return localIPs;
    }

    /**
     * 私有构造函数NetUtil
     */
    private NetUtil() {

    }

    /**
     * public static void main(String[] args) throws Exception {
     * System.out.println(StringUtils.collectionToDelimitedString(getAllLocalIP(), ",")); }
     */
}
